<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;

class GoodsTag extends AdminController
{
    public function onIndex(){
        $this->power('shop.goodsTag');
        $this->pageTitle = '标签管理';
        $this->pageIcon = 'fa-tag';
        return $this->display();
    }


    public function onGroupList(){
        $GoodsTagGroup =  $this->model('GoodsTagGroup');
        return $GoodsTagGroup->order('sort','desc')->getData();
    }

   
    public function onSaveGroup(){
        $data['title'] = $this->post('title',$this->check()->type('string')->required());

        $id  =  $this->post('group_id',$this->check()->type('int'));

        $GoodsTagGroup =  $this->model('GoodsTagGroup');

        if ($id){
            $GoodsTagGroup->updateId($id,$data);
            $this->log('修改标签组',$data);
        }else{
            $data['sort'] = $GoodsTagGroup->max('sort')+1;
            $GoodsTagGroup->insert($data);
            $this->log('添加标签组',$data);
        }

        return true;
    }

    public function onGroupCold(){
        
        $id =  $this->post('id',$this->check()->type('int')->required());
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        $GoodsTagGroup =  $this->model('GoodsTagGroup');
        if ($GoodsTagGroup->updateId($id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }

    public function onDelGroup(){
        $id = $this->post('group_id',$this->check()->type('int')->required());
        $GoodsTagGroup =  $this->model('GoodsTagGroup');
        $oldData = $GoodsTagGroup->getId($id);
        if (!$oldData){
            throw new \Exception('数据不存在');
        }
        if ($GoodsTagGroup->deleteId($id)){
            return true;
        }
    }

    public function onGroupSort(){
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $GoodsTagGroup =  $this->model('GoodsTagGroup');

        $GoodsTagGroup->switchSort($ids,'sort','desc');

        return true;
    }
    
    public function onList(){
        
        $group_id = $this->post('group_id',$this->check()->type('int'));
        $keyword = $this->post('keyword',$this->check()->type('string'));
        $page = $this->post('page',$this->check()->type('int')->default(1));
        
        $GoodsTag =  $this->model('GoodsTag');
        
        if ($group_id){
            $GoodsTag->where('group_id',$group_id);
        }

        if ($keyword){
            $GoodsTag->where('title','like','%'.$keyword.'%');
        }
        $GoodsTag->order('tag_id','desc');

        $list = $GoodsTag->page($page,40)->getData();

        return ['list'=>$list,'total'=>$GoodsTag->total,'pageCount'=>$GoodsTag->pageCount];
    }

    public function onSaveTag(){
        $data['title'] = $this->post('title',$this->check()->type('string')->required());
        $data['group_id'] = $this->post('group_id',$this->check()->type('int')->required());

        $id  =  $this->post('tag_id',$this->check()->type('int'));

        $GoodsTag =  $this->model('GoodsTag');

        if ($id){
            $GoodsTag->updateId($id,$data);
            $this->log('修改标签',$data);
        }else{
            $GoodsTag->insert($data);
            $this->log('添加标签',$data);
        }

        return true;
    }

    public function onDelTag(){
        $id = $this->post('tag_id',$this->check()->type('int')->required());
        $GoodsTag =  $this->model('GoodsTag');
        $oldData = $GoodsTag->getId($id);
        if (!$oldData){
            throw new \Exception('数据不存在');
        }
        if ($GoodsTag->deleteId($id)){
            $this->log('删除标签',$oldData);
            return true;
        }else{
            throw new \Exception('删除失败');
        }
    }

    public function onTagSort(){
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $GoodsTag =  $this->model('GoodsTag');

        $GoodsTag->switchSort($ids,'sort','desc');

        return true;
    }
}